package com.stars.empoly.activity

import android.os.Bundle
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.adapter.BanlanceAdapter
import com.stars.empoly.base.MBActivity
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.databinding.ActivityBalanceBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.model.RechargeBean
import com.stars.empoly.utils.PopuChosePayUtils
import com.stars.empoly.vmodel.BalanceVModel

/**
 * 充值
 *on 2022/9/23
 */
class BalanceActivity : MBActivity<BalanceVModel, ActivityBalanceBinding>() {


    var adapter: BanlanceAdapter? = null

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.getUserInfo()
        //获取充值的列表
        vm.getReCharge()
        vm.list.observe(this) {
            adapter = BanlanceAdapter()
            binding.recyclerView.adapter = adapter
            adapter?.clearList()
            adapter?.addData(it)
            adapter?.setOnItemClickListener { adapter, view, position ->

                var list = adapter.data as List<RechargeBean>
                for ((index, item) in list.withIndex()) {
                    if (index == position) {
                        vm.payId = item.id
                        vm.payMoney=item.money
                        vm.money=null
                        item.chose = true
                        adapter.notifyItemChanged(position)
                    } else {
                        if (item.chose) {
                            item.chose = false
                            adapter.notifyItemChanged(position)
                        }
                    }
                }


            }

        }
        vm.isType.observe(this) {
            var list = adapter?.data as List<RechargeBean>
            for ((index, item) in list.withIndex()) {
                if (item.chose) {
                    item.chose = false
                    adapter?.notifyItemChanged(index)
                }

            }
        }
    }

    override fun listeners() {
        super.listeners()



        binding.butSubmit.onClick {

            if(vm.payId.isNullOrEmpty()&&vm.money.isNullOrEmpty()){
                toast("请设置充值金额")
                return@onClick
            }
            var money:String?=""
            money = if(!vm.payMoney.isNullOrEmpty()){
                vm.payMoney
            }else{
                vm.money
            }

            PopuChosePayUtils.show(context(),money, binding.butSubmit, object : ChoiceData {
                override fun onRestuse(type: String?) {

                    vm.toPay(type)
                }

            })
        }
    }

    override fun vmClass(): Class<BalanceVModel> {
        return BalanceVModel::class.java
    }

    override fun binding(): ActivityBalanceBinding {
        return ActivityBalanceBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "余额"
    }

    override val isRxbus: Boolean
        get() =true

    @Subscribe(tags = [Tag(ConfigRx.PAY_COMPLETE)])
    fun setPayComplete(type: String) {
       toast("支付完成")

        vm.getUserInfo()
        //(repository().context as Activity).finish()
        //刷新
        RxBus.get().post(ConfigRx.REFRESH_USER, "")

    }

    @Subscribe(tags = [Tag(ConfigRx.PAY_ERROR)])
    fun setPayError(type: String) {



            toast("支付失败")

    }

}
